import { fetchHome } from '../../services/home/home';
import { fetchGoodsList } from '../../services/good/fetchGoods';

Page({
  data: {
    imgSrcs: [],
    tabList: [],
    goodsList: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    current: 0,
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: { type: 'dots' },
    swiperImageProps: { mode: 'aspectFill' },
    db: {},
    tabs: 0,
  },

  goodListPagination: {
    index: 0,
    num: 5,
  },

  privateData: {
    tabIndex: 0,
  },

  async onLoad() {
    wx.removeStorageSync('active')

    this.db = wx.cloud.database();
    this.init();
  },
  onShow() {
    this.getTabBar().init();
  },

  onReachBottom() {
    // if (this.data.goodsListLoadStatus === 0) {
    //   this.loadGoodsList();
    // }
  },

  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.loadHomePage();
  },

  async loadHomePage() {
    this.setData({
      pageLoading: true,
    });
    await this.getTabList();
    await this.getGoodList();
    fetchHome().then(({ swiper }) => {
      this.setData({
        imgSrcs: swiper,
        pageLoading: false,
      });
      // this.loadGoodsList()
    });
    wx.stopPullDownRefresh();
  },
  // 获取tabList
  async getTabList() {
    const { data: tabList } = await this.db.collection('tabList').get();
    this.setData({
      tabList,
    });
    return tabList;
  },
  // 获取所有餐单
  async getGoodList() {
    const searchInfo = {
      tag: this.data.tabs,
    };
    const res = await this.db.collection('goodList').where(searchInfo).get();
    this.setData({
      goodsList: res.data,
    });
    return res.data;
  },

  tabChangeHandle(e) {
    this.setData({
      tabs: e.detail.value,
    });
    this.getGoodList();
  },

  onReTry() {
    this.loadGoodsList();
  },

  async loadGoodsList(fresh = false) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
    }

    this.setData({ goodsListLoadStatus: 1 });

    const pageSize = this.goodListPagination.num;
    let pageIndex = this.privateData.tabIndex * pageSize + this.goodListPagination.index + 1;
    if (fresh) {
      pageIndex = 0;
    }

    try {
      const nextList = await fetchGoodsList(pageIndex, pageSize, this.privateData.tabIndex);
      console.log(nextList, fresh);
      this.setData({
        goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
        goodsListLoadStatus: 0,
      });

      this.goodListPagination.index = pageIndex;
      this.goodListPagination.num = pageSize;
    } catch (err) {
      this.setData({ goodsListLoadStatus: 3 });
    }
  },

  goodListClickHandle(e) {
    const { index } = e.detail;
    const { _id: spuId } = this.data.goodsList[index];
    wx.navigateTo({
      url: `/pages/goods/details/index?spuId=${spuId}`,
    });
  },

  navToSearchPage() {
    wx.navigateTo({ url: '/pages/goods/search/index' });
  },

  navToActivityDetail({ detail }) {
    return
    const { index: promotionID = 0 } = detail || {};
    wx.navigateTo({
      url: `/pages/promotion-detail/index?promotion_id=${promotionID}`,
    });
  },
});
